let fs = require('fs')
let path = require('path')

function basePagesJson() {
    return {
        "pages": [],
        "globalStyle": {
            navigationStyle: "custom",
            renderingMode: 'seperated'
        },
        easycom: {
            autoscan: true,
            custom: {
                "^uni-(.*)": "@dcloudio/uni-ui/lib/uni-$1/uni-$1.vue"
            }
        },
        condition: {
            current: 0,
            list: [
                {
                    name: 'index',
                    path: 'pages/index/index',
                    query: ""
                },
                {
                    name: 'my',
                    path: 'pages/my/my',
                    query: ""
                }
            ]
        },
        subPackages: []
    }
}


function getFilePath(dir, result = []) {
    let filesOrDirs = fs.readdirSync(dir)
    for (let fileOrDir of filesOrDirs) {
        let resultPath = path.join(dir, fileOrDir)
        if (fs.lstatSync(resultPath).isDirectory()) {
            getFilePath(resultPath, result)
            continue
        }
        let regExp = /\w+\/([\w\/]+).vue$/
        result.push(regExp.exec(resultPath)[1])
    }
    return result
}

function makePages(pagesJson) {
    pagesJson.pages = getFilePath('./src/pages').map((path) => {
        return {
            path,
            style: {
                disableScroll: true,
                enablePullDownRefresh: false
            }
        }
    })
}

function getSubRoots(subDir, subPackages) {
    let subs = fs.readdirSync(subDir)
    return subs.map(item => {
        return path.join(subDir, item)
    })
}

function removePathTop(pathStr) {
    let regExp = /\w+?\/(.+)/
    return regExp.exec(pathStr)[1]
}

function makeSubPackages(pagesJson) {
    let subRoots = getSubRoots('./src/sub-package', )
    pagesJson.subPackages = subRoots.map(item => {
        return {
            root: removePathTop(item),
            pages: getFilePath(item).map(item => {
                return {
                    path: removePathTop(item),
                    disableScroll: true,
                    enablePullDownRefresh: false
                }
            })
        }
    })
}

function pagesJsonInit() {
    let basePagesJsonTemp = basePagesJson()

    makePages(basePagesJsonTemp)
    makeSubPackages(basePagesJsonTemp)

    fs.truncateSync('./src/pages.json', 0)
    fs.writeFileSync('./src/pages.json', JSON.stringify(basePagesJsonTemp, undefined, 4))
}
pagesJsonInit()

module.exports = {
    transpileDependencies:['@dcloudio/uni-ui'],
    chainWebpack(config) {
        // 发行或运行时启用了压缩时会生效
        config.optimization.minimizer('terser').tap((args) => {
            const compress = args[0].terserOptions.compress
            // 非 App 平台移除 console 代码(包含所有 console 方法，如 log,debug,info...)
            compress.drop_console = true
            compress.pure_funcs = [
                '__f__', // App 平台 vue 移除日志代码
                // 'console.debug' // 可移除指定的 console 方法
            ]
            return args
        })
    }
}